package priv.bruce.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import priv.bruce.entry.MovieComment;
import priv.bruce.entry.MovieScore;
import priv.bruce.entry.UserInfo;
import priv.bruce.util.HandlerSet;
import priv.bruce.util.JDBCUtil;

public class Comment4DetailByZanDescService implements HandlerSet<MovieComment>{
	public Map<String, Object> findCommentByZan(int id) throws Exception {
//		下面是一个通过点赞数降序排列显示最热的评论
		String sql4Comment = "select comment_content,comment_time,comment_zan,comment_id,user_id from t_comment where movie_id=? "
				+ "order by comment_zan DESC,comment_time DESC";
		Map<String, Object> map = new HashMap<>();
		List<MovieComment> movieComments = JDBCUtil.query(sql4Comment,this,id);
		map.put("commentInfo", movieComments);
//		下面是将用户名连上评论这个表
		String sql4Username = "select user_name from t_user as u join t_comment as c "
				+ "on u.user_id=c.user_id where movie_id=? order by c.comment_zan DESC,comment_time DESC";
		List<UserInfo> userInfo = JDBCUtil.query(sql4Username, new HandlerSet<UserInfo>() {
			@Override
			public UserInfo handler(ResultSet rs) throws SQLException {
				UserInfo userInfo = new UserInfo();
				userInfo.setUser_name(rs.getString("user_name"));
				return userInfo;
			}
		},id);
		map.put("userInfo", userInfo);
//		下面是将评分连上评论这个表
		String sql4Score = "select score,score_id from t_score as s join t_comment as c "
				+ "on s.user_id=c.user_id where s.movie_id=? and c.movie_id=? order by c.comment_zan DESC,comment_time DESC";
		List<MovieScore> movieScore = JDBCUtil.query(sql4Score, new HandlerSet<MovieScore>() {
			@Override
			public MovieScore handler(ResultSet rs) throws SQLException {
				MovieScore movieScore = new MovieScore();
				movieScore.setScore(rs.getInt("score"));
				movieScore.setScore_id(rs.getInt("score_id"));
				return movieScore;
			}
		},id,id);
		map.put("movieScore", movieScore);
		return map;
	}
	
//	下面是上文的This方法，获得表中的评论内容、时间、点赞量
	@Override
	public MovieComment handler(ResultSet rs) throws SQLException {
		MovieComment comment = new MovieComment();
		comment.setComment_content(rs.getString("comment_content"));
		comment.setComment_time(rs.getTimestamp("comment_time"));
		comment.setComment_zan(rs.getInt("comment_zan"));
		comment.setComment_id(rs.getInt("comment_id"));
		comment.setUser_id(rs.getInt("user_id"));
		return comment;
	}
}
